﻿using System;
using System.Data;
using System.Data.Common;
using System.Data.EffiProzDB;
using System.Diagnostics;

namespace EffiProzDBServer
{
    class Program
    {
        static void Main(string[] args)
        {

            int insertCount = 100000;
            string connString = "Type=Memory ; Data Source=TestDB; User=sa; Password=;";

            using (DbConnection conn = new EffiProzDBConnection(connString))
            {

                conn.Open();
                DbCommand command = conn.CreateCommand();
                command.CommandText = "DROP TABLE TEST IF EXISTS;";
                command.ExecuteNonQuery();
                command.CommandText = "CREATE TABLE TEST(ID int, NAME varchar(255));";
                command.ExecuteNonQuery();

                command.CommandText = "INSERT INTO TEST(ID , NAME ) VALUES(@ID , @Name);";
                command.Prepare();

                DbParameter param1 = command.CreateParameter();
                param1.ParameterName = "@ID";
                DbParameter param2 = command.CreateParameter();
                param2.ParameterName = "@Name";
                command.Parameters.Add(param1);
                command.Parameters.Add(param2);

                Stopwatch sw = new Stopwatch();
                Random rand = new Random();

                sw.Start();
                for (int i = 0; i < insertCount; i++)
                {
                    param1.Value = rand.Next(insertCount);
                    param2.Value = "MyName" + param1.Value;
                    command.ExecuteNonQuery();
                }

                sw.Stop();

                Console.WriteLine("EffiProz Time per Insert (ms) : " + ((double)sw.ElapsedMilliseconds / insertCount));

                int searchCount = 10;
                command = conn.CreateCommand();
                command.CommandText = "UPDATE TEST SET NAME='Updated' WHERE ID=@ID;";
                command.Prepare();

                param1 = command.CreateParameter();
                param1.ParameterName = "@ID";
                command.Parameters.Add(param1);

                sw.Reset(); sw.Start();
                DbDataReader reader;
                for (int i = 0; i < searchCount; i++)
                {
                    param1.Value = rand.Next(insertCount);
                    reader = command.ExecuteReader();
                    while (reader.Read()) ;
                }

                sw.Stop();
                Console.WriteLine("EffiProz Time per Search (ms) :" + ((double)sw.ElapsedMilliseconds / searchCount));
            }


            Console.ReadKey(true);
        }
    }
}
